﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
//using System.Data.Linq.Mapping;
using System.Data;
using System.Linq;
using System.Web;


//using System.Reflection;
//using System.Linq.Expressions;
using System.Web.Security;

using MVCWeb.Models;

namespace MVCWeb.Controllers
{
    /// <summary>
    /// Utility class to store compiled queries
    /// </summary>
    public static class QueriesUtility
    {
        /// <summary>
        /// Gets the query that returns categories with more than five products.
        /// </summary>
        /// <value>The query containing categories with more than five products.</value>
        /// using (HOACRMDataContext context = new HOACRMDataContext())
        /// {
        ///     QueriesUtility.GetCategoriesWithMoreThanFiveProducts(context);
        /// }
        public static Func<HOACRMDataContext, IEnumerable<DocCategory>> GetAllDocCategories
        {
            get
            {
                Func<HOACRMDataContext, IEnumerable<DocCategory>> func =
                  CompiledQuery.Compile<HOACRMDataContext, IEnumerable<DocCategory>>
                  ((HOACRMDataContext context) => context.DocCategories);
                return func;
            }
        }

        //public static Func<HOACRMDataContext,User> GetCurrentUser()
        //{
        //    MembershipUser membershipUser = Membership.GetUser();
        //    Guid membershipId = new Guid(membershipUser.ProviderUserKey.ToString());
        //    MVCWeb.Models.User user = CompiledQuery.Compile<HOACRMDataContext, User>
        //      ((HOACRMDataContext context) => context.Users.Where<User>(u => u.MembershipId == membershipId).First<User>());
        //    return func;
        //}

        public static Func<HOACRMDataContext,Community> GetCommunityFromUserId(int userid)
        {
            Func<HOACRMDataContext, Community> func =
              CompiledQuery.Compile<HOACRMDataContext, Community>
              ((HOACRMDataContext context) => context.Communities.Where<Community>(c => c.UserId == userid  || c.ManagerId == userid).First<Community>());
            return func;
        }
    }
}

//// Get community by login user.
//MembershipUser membershipUser = Membership.GetUser();
//Guid membershipId = new Guid(membershipUser.ProviderUserKey.ToString());
//MVCWeb.Models.User user = dc.Users.Where<MVCWeb.Models.User>(u => u.MembershipId == membershipId).First<MVCWeb.Models.User>();
//Community community = dc.Communities.Where<Community>(c => c.UserId == user.UserId || c.ManagerId == user.UserId).First<Community>();
